# 杭州电子科技大学
# 王志文
# 创建时间 2021/10/14 17:45
import numpy as np
from skimage import io
import cv2
from matplotlib import pyplot as plt
path = "D:\Pycharm\现代测试大作业\Lena.png"
I = io.imread(path) #R*0.299+G*0.587+B*0.114
def medianfilter(image, winsize):
    rows, cols, channel = image.shape
    winH, winW = winsize
    halfwinH = (winH-1)//2 +1
    halfwinW = (winW-1)//2 +1
    medianfilterimage = np.zeros(image.shape, image.dtype)
    for k in range(channel):
        for i in range(rows):
            for j in range(cols):
                rtop = 0 if i-halfwinH < 0 else i - halfwinH
                rbootom = rows-1 if i + halfwinH > rows -1 else i + halfwinH
                cleft = 0 if j - halfwinW < 0 else j - halfwinW
                cright = cols-1 if j + halfwinW > cols -1 else j + halfwinW
                region = image[rtop:rbootom+1, cleft:cright+1, k]
                medianfilterimage[i][j][k] = np.median(region)
    return medianfilterimage
window = (9, 9)
output = medianfilter(I, window)
